<?php
/**
 * Created by PhpStorm.
 * User: reshared
 * Date: 2017/9/28
 * Time: 下午1:47
 */

namespace Index\Repository;


abstract class BaseRepository
{
    /**
     * model集合
     * @var array
     */
    protected $models = [];

    /**
     * event集合
     * @var array
     */
    protected $events = [];

    /**
     * 自动创建,并保存model对象
     * @param $name
     * @return mixed
     */
    protected function model($name)
    {
        if (!key_exists($name, $this->models)) {
            $modelName = "Index\\Model\\".ucfirst($name). 'Model';
            $this->models[$name] = new $modelName();
        }

        return $this->models[$name];
    }

    /**
     * 自动创建,病保存event对象
     * @param $name
     * @return mixed
     */
    public function event($name)
    {
        if (!key_exists($name, $this->events)) {
            $eventName = "Index\\Event\\".ucfirst($name). 'Event';
            $this->events[$name] = new $eventName();
        }

        return $this->events[$name];
    }
}